added Feb 2001 SDK
[windows-sources.git] / shared source / wpf / src / host / dll / bootstrapper.cxx
blob2bffb31513f871c6c98ecc266eb87a95db977f8c
1 //+-----------------------------------------------------------------------
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // Description:
6 // Implements the BootStrapper class of PresentationHost.
7 //
8 // History
9 // 2002/06/25-murrayw
10 // Created
11 // 2003/06/30-[....]
12 // Ported ByteRangeDownloader to WCP
13 // 2007/09/20-[....]
14 // Ported Windows->DevDiv. See SourcesHistory.txt.
16 //------------------------------------------------------------------------
18 #include "Precompiled.hxx"
19 #include "Bootstrapper.hxx"
20 #include "OleDocument.hxx"
21 #include <fxver.h>
23 //******************************************************************************
25 // The following definitions are obtained by doing a #import of mscorlib.dll and
26 // copying over the required interface definitions. If there are any breaking
27 // changes to these interfaces, we need to regenerate the definitions.
29 //******************************************************************************
31 struct __declspec(uuid("05f696dc-2b29-3663-ad8b-c4389cf2a713"))
32 _AppDomain : IDispatch
35 // Raw methods provided by interface
38 virtual HRESULT __stdcall get_ToString (
39 /*[out,retval]*/ BSTR * pRetVal ) = 0;
40 virtual HRESULT __stdcall Equals (
41 /*[in]*/ VARIANT other,
42 /*[out,retval]*/ VARIANT_BOOL * pRetVal ) = 0;
43 virtual HRESULT __stdcall GetHashCode (
44 /*[out,retval]*/ long * pRetVal ) = 0;
45 virtual HRESULT __stdcall GetType (
46 /*[out,retval]*/ struct _Type * * pRetVal ) = 0;
47 virtual HRESULT __stdcall InitializeLifetimeService (
48 /*[out,retval]*/ VARIANT * pRetVal ) = 0;
49 virtual HRESULT __stdcall GetLifetimeService (
50 /*[out,retval]*/ VARIANT * pRetVal ) = 0;
51 virtual HRESULT __stdcall get_Evidence (
52 /*[out,retval]*/ struct _Evidence * * pRetVal ) = 0;
53 virtual HRESULT __stdcall add_DomainUnload (
54 /*[in]*/ struct _EventHandler * value ) = 0;
55 virtual HRESULT __stdcall remove_DomainUnload (
56 /*[in]*/ struct _EventHandler * value ) = 0;
57 virtual HRESULT __stdcall add_AssemblyLoad (
58 /*[in]*/ struct _AssemblyLoadEventHandler * value ) = 0;
59 virtual HRESULT __stdcall remove_AssemblyLoad (
60 /*[in]*/ struct _AssemblyLoadEventHandler * value ) = 0;
61 virtual HRESULT __stdcall add_ProcessExit (
62 /*[in]*/ struct _EventHandler * value ) = 0;
63 virtual HRESULT __stdcall remove_ProcessExit (
64 /*[in]*/ struct _EventHandler * value ) = 0;
65 virtual HRESULT __stdcall add_TypeResolve (
66 /*[in]*/ struct _ResolveEventHandler * value ) = 0;
67 virtual HRESULT __stdcall remove_TypeResolve (
68 /*[in]*/ struct _ResolveEventHandler * value ) = 0;
69 virtual HRESULT __stdcall add_ResourceResolve (
70 /*[in]*/ struct _ResolveEventHandler * value ) = 0;
71 virtual HRESULT __stdcall remove_ResourceResolve (
72 /*[in]*/ struct _ResolveEventHandler * value ) = 0;
73 virtual HRESULT __stdcall add_AssemblyResolve (
74 /*[in]*/ struct _ResolveEventHandler * value ) = 0;
75 virtual HRESULT __stdcall remove_AssemblyResolve (
76 /*[in]*/ struct _ResolveEventHandler * value ) = 0;
77 virtual HRESULT __stdcall add_UnhandledException (
78 /*[in]*/ struct _UnhandledExceptionEventHandler * value ) = 0;
79 virtual HRESULT __stdcall remove_UnhandledException (
80 /*[in]*/ struct _UnhandledExceptionEventHandler * value ) = 0;
81 virtual HRESULT __stdcall DefineDynamicAssembly (
82 /*[in]*/ struct _AssemblyName * name,
83 /*[in]*/ enum AssemblyBuilderAccess access,
84 /*[out,retval]*/ struct _AssemblyBuilder * * pRetVal ) = 0;
85 virtual HRESULT __stdcall DefineDynamicAssembly_2 (
86 /*[in]*/ struct _AssemblyName * name,
87 /*[in]*/ enum AssemblyBuilderAccess access,
88 /*[in]*/ BSTR dir,
89 /*[out,retval]*/ struct _AssemblyBuilder * * pRetVal ) = 0;
90 virtual HRESULT __stdcall DefineDynamicAssembly_3 (
91 /*[in]*/ struct _AssemblyName * name,
92 /*[in]*/ enum AssemblyBuilderAccess access,
93 /*[in]*/ struct _Evidence * Evidence,
94 /*[out,retval]*/ struct _AssemblyBuilder * * pRetVal ) = 0;
95 virtual HRESULT __stdcall DefineDynamicAssembly_4 (
96 /*[in]*/ struct _AssemblyName * name,
97 /*[in]*/ enum AssemblyBuilderAccess access,
98 /*[in]*/ struct _PermissionSet * requiredPermissions,
99 /*[in]*/ struct _PermissionSet * optionalPermissions,
100 /*[in]*/ struct _PermissionSet * refusedPermissions,
101 /*[out,retval]*/ struct _AssemblyBuilder * * pRetVal ) = 0;
102 virtual HRESULT __stdcall DefineDynamicAssembly_5 (
103 /*[in]*/ struct _AssemblyName * name,
104 /*[in]*/ enum AssemblyBuilderAccess access,
105 /*[in]*/ BSTR dir,
106 /*[in]*/ struct _Evidence * Evidence,
107 /*[out,retval]*/ struct _AssemblyBuilder * * pRetVal ) = 0;
108 virtual HRESULT __stdcall DefineDynamicAssembly_6 (
109 /*[in]*/ struct _AssemblyName * name,
110 /*[in]*/ enum AssemblyBuilderAccess access,
111 /*[in]*/ BSTR dir,
112 /*[in]*/ struct _PermissionSet * requiredPermissions,
113 /*[in]*/ struct _PermissionSet * optionalPermissions,
114 /*[in]*/ struct _PermissionSet * refusedPermissions,
115 /*[out,retval]*/ struct _AssemblyBuilder * * pRetVal ) = 0;
116 virtual HRESULT __stdcall DefineDynamicAssembly_7 (
117 /*[in]*/ struct _AssemblyName * name,
118 /*[in]*/ enum AssemblyBuilderAccess access,
119 /*[in]*/ struct _Evidence * Evidence,
120 /*[in]*/ struct _PermissionSet * requiredPermissions,
121 /*[in]*/ struct _PermissionSet * optionalPermissions,
122 /*[in]*/ struct _PermissionSet * refusedPermissions,
123 /*[out,retval]*/ struct _AssemblyBuilder * * pRetVal ) = 0;
124 virtual HRESULT __stdcall DefineDynamicAssembly_8 (
125 /*[in]*/ struct _AssemblyName * name,
126 /*[in]*/ enum AssemblyBuilderAccess access,
127 /*[in]*/ BSTR dir,
128 /*[in]*/ struct _Evidence * Evidence,
129 /*[in]*/ struct _PermissionSet * requiredPermissions,
130 /*[in]*/ struct _PermissionSet * optionalPermissions,
131 /*[in]*/ struct _PermissionSet * refusedPermissions,
132 /*[out,retval]*/ struct _AssemblyBuilder * * pRetVal ) = 0;
133 virtual HRESULT __stdcall DefineDynamicAssembly_9 (
134 /*[in]*/ struct _AssemblyName * name,
135 /*[in]*/ enum AssemblyBuilderAccess access,
136 /*[in]*/ BSTR dir,
137 /*[in]*/ struct _Evidence * Evidence,
138 /*[in]*/ struct _PermissionSet * requiredPermissions,
139 /*[in]*/ struct _PermissionSet * optionalPermissions,
140 /*[in]*/ struct _PermissionSet * refusedPermissions,
141 /*[in]*/ VARIANT_BOOL IsSynchronized,
142 /*[out,retval]*/ struct _AssemblyBuilder * * pRetVal ) = 0;
143 virtual HRESULT __stdcall CreateInstance (
144 /*[in]*/ BSTR AssemblyName,
145 /*[in]*/ BSTR typeName,
146 /*[out,retval]*/ struct _ObjectHandle * * pRetVal ) = 0;
147 virtual HRESULT __stdcall CreateInstanceFrom (
148 /*[in]*/ BSTR assemblyFile,
149 /*[in]*/ BSTR typeName,
150 /*[out,retval]*/ struct _ObjectHandle * * pRetVal ) = 0;
151 virtual HRESULT __stdcall CreateInstance_2 (
152 /*[in]*/ BSTR AssemblyName,
153 /*[in]*/ BSTR typeName,
154 /*[in]*/ SAFEARRAY * activationAttributes,
155 /*[out,retval]*/ struct _ObjectHandle * * pRetVal ) = 0;
156 virtual HRESULT __stdcall CreateInstanceFrom_2 (
157 /*[in]*/ BSTR assemblyFile,
158 /*[in]*/ BSTR typeName,
159 /*[in]*/ SAFEARRAY * activationAttributes,
160 /*[out,retval]*/ struct _ObjectHandle * * pRetVal ) = 0;
161 virtual HRESULT __stdcall CreateInstance_3 (
162 /*[in]*/ BSTR AssemblyName,
163 /*[in]*/ BSTR typeName,
164 /*[in]*/ VARIANT_BOOL ignoreCase,
165 /*[in]*/ enum BindingFlags bindingAttr,
166 /*[in]*/ struct _Binder * Binder,
167 /*[in]*/ SAFEARRAY * args,
168 /*[in]*/ struct _CultureInfo * culture,
169 /*[in]*/ SAFEARRAY * activationAttributes,
170 /*[in]*/ struct _Evidence * securityAttributes,
171 /*[out,retval]*/ struct _ObjectHandle * * pRetVal ) = 0;
172 virtual HRESULT __stdcall CreateInstanceFrom_3 (
173 /*[in]*/ BSTR assemblyFile,
174 /*[in]*/ BSTR typeName,
175 /*[in]*/ VARIANT_BOOL ignoreCase,
176 /*[in]*/ enum BindingFlags bindingAttr,
177 /*[in]*/ struct _Binder * Binder,
178 /*[in]*/ SAFEARRAY * args,
179 /*[in]*/ struct _CultureInfo * culture,
180 /*[in]*/ SAFEARRAY * activationAttributes,
181 /*[in]*/ struct _Evidence * securityAttributes,
182 /*[out,retval]*/ struct _ObjectHandle * * pRetVal ) = 0;
183 virtual HRESULT __stdcall Load (
184 /*[in]*/ struct _AssemblyName * assemblyRef,
185 /*[out,retval]*/ struct _Assembly * * pRetVal ) = 0;
186 virtual HRESULT __stdcall Load_2 (
187 /*[in]*/ BSTR assemblyString,
188 /*[out,retval]*/ struct _Assembly * * pRetVal ) = 0;
189 virtual HRESULT __stdcall Load_3 (
190 /*[in]*/ SAFEARRAY * rawAssembly,
191 /*[out,retval]*/ struct _Assembly * * pRetVal ) = 0;
192 virtual HRESULT __stdcall Load_4 (
193 /*[in]*/ SAFEARRAY * rawAssembly,
194 /*[in]*/ SAFEARRAY * rawSymbolStore,
195 /*[out,retval]*/ struct _Assembly * * pRetVal ) = 0;
196 virtual HRESULT __stdcall Load_5 (
197 /*[in]*/ SAFEARRAY * rawAssembly,
198 /*[in]*/ SAFEARRAY * rawSymbolStore,
199 /*[in]*/ struct _Evidence * securityEvidence,
200 /*[out,retval]*/ struct _Assembly * * pRetVal ) = 0;
201 virtual HRESULT __stdcall Load_6 (
202 /*[in]*/ struct _AssemblyName * assemblyRef,
203 /*[in]*/ struct _Evidence * assemblySecurity,
204 /*[out,retval]*/ struct _Assembly * * pRetVal ) = 0;
205 virtual HRESULT __stdcall Load_7 (
206 /*[in]*/ BSTR assemblyString,
207 /*[in]*/ struct _Evidence * assemblySecurity,
208 /*[out,retval]*/ struct _Assembly * * pRetVal ) = 0;
209 virtual HRESULT __stdcall ExecuteAssembly (
210 /*[in]*/ BSTR assemblyFile,
211 /*[in]*/ struct _Evidence * assemblySecurity,
212 /*[out,retval]*/ long * pRetVal ) = 0;
213 virtual HRESULT __stdcall ExecuteAssembly_2 (
214 /*[in]*/ BSTR assemblyFile,
215 /*[out,retval]*/ long * pRetVal ) = 0;
216 virtual HRESULT __stdcall ExecuteAssembly_3 (
217 /*[in]*/ BSTR assemblyFile,
218 /*[in]*/ struct _Evidence * assemblySecurity,
219 /*[in]*/ SAFEARRAY * args,
220 /*[out,retval]*/ long * pRetVal ) = 0;
221 virtual HRESULT __stdcall get_FriendlyName (
222 /*[out,retval]*/ BSTR * pRetVal ) = 0;
223 virtual HRESULT __stdcall get_BaseDirectory (
224 /*[out,retval]*/ BSTR * pRetVal ) = 0;
225 virtual HRESULT __stdcall get_RelativeSearchPath (
226 /*[out,retval]*/ BSTR * pRetVal ) = 0;
227 virtual HRESULT __stdcall get_ShadowCopyFiles (
228 /*[out,retval]*/ VARIANT_BOOL * pRetVal ) = 0;
229 virtual HRESULT __stdcall GetAssemblies (
230 /*[out,retval]*/ SAFEARRAY * * pRetVal ) = 0;
231 virtual HRESULT __stdcall AppendPrivatePath (
232 /*[in]*/ BSTR Path ) = 0;
233 virtual HRESULT __stdcall ClearPrivatePath ( ) = 0;
234 virtual HRESULT __stdcall SetShadowCopyPath (
235 /*[in]*/ BSTR s ) = 0;
236 virtual HRESULT __stdcall ClearShadowCopyPath ( ) = 0;
237 virtual HRESULT __stdcall SetCachePath (
238 /*[in]*/ BSTR s ) = 0;
239 virtual HRESULT __stdcall SetData (
240 /*[in]*/ BSTR name,
241 /*[in]*/ VARIANT data ) = 0;
242 virtual HRESULT __stdcall GetData (
243 /*[in]*/ BSTR name,
244 /*[out,retval]*/ VARIANT * pRetVal ) = 0;
245 virtual HRESULT __stdcall SetAppDomainPolicy (
246 /*[in]*/ struct _PolicyLevel * domainPolicy ) = 0;
247 virtual HRESULT __stdcall SetThreadPrincipal (
248 /*[in]*/ struct IPrincipal * principal ) = 0;
249 virtual HRESULT __stdcall SetPrincipalPolicy (
250 /*[in]*/ enum PrincipalPolicy policy ) = 0;
251 virtual HRESULT __stdcall DoCallBack (
252 /*[in]*/ struct _CrossAppDomainDelegate * theDelegate ) = 0;
253 virtual HRESULT __stdcall get_DynamicDirectory (
254 /*[out,retval]*/ BSTR * pRetVal ) = 0;
258 struct __declspec(uuid("ea675b47-64e0-3b5f-9be7-f7dc2990730d"))
259 _ObjectHandle : IDispatch
262 // Raw methods provided by interface
265 virtual HRESULT __stdcall get_ToString (
266 /*[out,retval]*/ BSTR * pRetVal ) = 0;
267 virtual HRESULT __stdcall Equals (
268 /*[in]*/ VARIANT obj,
269 /*[out,retval]*/ VARIANT_BOOL * pRetVal ) = 0;
270 virtual HRESULT __stdcall GetHashCode (
271 /*[out,retval]*/ long * pRetVal ) = 0;
272 virtual HRESULT __stdcall GetType (
273 /*[out,retval]*/ struct _Type * * pRetVal ) = 0;
274 virtual HRESULT __stdcall GetLifetimeService (
275 /*[out,retval]*/ VARIANT * pRetVal ) = 0;
276 virtual HRESULT __stdcall InitializeLifetimeService (
277 /*[out,retval]*/ VARIANT * pRetVal ) = 0;
278 virtual HRESULT __stdcall CreateObjRef (
279 /*[in]*/ struct _Type * requestedType,
280 /*[out,retval]*/ struct _ObjRef * * pRetVal ) = 0;
281 virtual HRESULT __stdcall Unwrap (
282 /*[out,retval]*/ VARIANT * pRetVal ) = 0;
286 //******************************************************************************
288 // BootStrapper::BootStrapper
290 //******************************************************************************
292 BootStrapper::BootStrapper(__in COleDocument* pOleDoc)
294 m_pOleDoc = pOleDoc;
295 m_pHost = NULL;
298 BootStrapper::~BootStrapper()
300 if (m_pHost)
302 m_pHost->Stop();
303 ReleaseInterface(m_pHost);
305 m_pOleDoc = NULL;
308 //******************************************************************************
310 // BootStrapper::AvalonExecute()
312 //******************************************************************************
314 void BootStrapper::AvalonExecute(__in_opt IStream *loadStream /*=NULL*/)
316 HRESULT hr = S_OK;
318 // Interface pointers
319 IUnknown *pAppDomainPunk = NULL;
320 _AppDomain *pDefaultDomain = NULL;
322 // Application pointers
323 _ObjectHandle *pObjHandle = NULL;
324 VARIANT v = {0};
326 // other pointers
327 BSTR bstrAssembly = NULL;
328 BSTR bstrType = NULL;
330 // CLR hosting interfaces
331 ICLRMetaHostPolicy *pMetaHostPolicy = NULL;
332 ICLRRuntimeInfo *pRuntimeInfo = NULL;
333 ICLRRuntimeHost *pHost = NULL;
334 ICLRControl *pCLRControl = NULL;
335 ICLRDomainManager *pCLRDomainManager = NULL;
337 // Configuration properties for the default AppDomain
339 // Currently we set PARTIAL_TRUST_VISIBLE_ASSEMBLIES to the empty list, which tells the CLR that no
340 // conditionally APTCA assemblies are to be enabled in the default AppDomain. Since this matches what
341 // will be the case in the sandboxed AppDomains that will be spun up, the CLR can use this to optimize
342 // away the need for some transitive closure walks when sharing code, and also allows us to share code
343 // between the default domain and sandboxed domains.
344 LPCWSTR pwszDefaultDomainProperties[] =
346 L"PARTIAL_TRUST_VISIBLE_ASSEMBLIES"
349 LPCWSTR pwszDefaultDomainPropertyValues[] =
354 // Compiler will optimize the multiple inline calls
355 if (m_pOleDoc->GetCurrentMime() != MimeType_Xps &&
356 m_pOleDoc->GetCurrentMime() != MimeType_Application &&
357 m_pOleDoc->GetCurrentMime() != MimeType_Markup)
359 CKHR(E_FAIL);
362 bstrAssembly = SysAllocString(L"PresentationFramework, Version=" LVER_ASSEMBLY_MANAGED L", Culture=neutral, PublicKeyToken=" LWCP_PUBLIC_KEY_TOKEN L", Custom=null");
363 bstrType = SysAllocString(L"System.Windows.Interop.DocObjHost");
365 CK_ALLOC(bstrAssembly);
366 CK_ALLOC(bstrType);
370 // Start the CLR.
372 Assert(m_pHost == NULL);
374 // Tracing Note: The shim wraps this function in VersionActivateStart-VersionActivateEnd events.
375 // The bulk of the time is spent in starting the CLR. In particular, ICorRuntimeHost::Start()
376 // causes PresentationFramework.dll and its dependencies to be loaded. This is because our
377 // AppDomain Manager comes from PF.
379 EventWriteWpfHostUm_StartingCLRStart();
381 // Use of the new CLR 4.0 MetaHost APIs to retrieve the latest CLR version within the 4.0.0 band.
382 // The METAHOST_POLICY_APPLY_UPGRADE_POLICY flag is what does the trick.
383 CKHR(CLRCreateInstance(CLSID_CLRMetaHostPolicy, IID_ICLRMetaHostPolicy, (LPVOID*)&pMetaHostPolicy));
385 wchar_t version[40] = L"v4.0.0";
386 DWORD cchVersion = ARRAYSIZE(version);
388 CKHR(pMetaHostPolicy->GetRequestedRuntime(METAHOST_POLICY_APPLY_UPGRADE_POLICY,
389 NULL, // assembly path
390 NULL, // configuration file stream
391 version, &cchVersion, // preferred CLR version
392 NULL, NULL, // image version
393 NULL, // config flags
394 IID_ICLRRuntimeInfo,
395 (LPVOID*)&pRuntimeInfo));
397 CKHR(pRuntimeInfo->SetDefaultStartupFlags(STARTUP_LOADER_OPTIMIZATION_MULTI_DOMAIN_HOST | STARTUP_CONCURRENT_GC,
398 NULL /* config file */));
399 CKHR(pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (LPVOID*)&pHost));
401 CKHR(pHost->GetCLRControl(&pCLRControl));
402 CKHR(pCLRControl->GetCLRManager(IID_ICLRDomainManager, (LPVOID*)&pCLRDomainManager));
403 CKHR(pCLRDomainManager->SetAppDomainManagerType(bstrAssembly,
404 L"System.Windows.Interop.PresentationAppDomainManager",
405 eInitializeNewDomainFlags_None));
406 CKHR(pCLRDomainManager->SetPropertiesForDefaultAppDomain(_countof(pwszDefaultDomainProperties),
407 pwszDefaultDomainProperties,
408 pwszDefaultDomainPropertyValues));
410 CKHR(pHost->Start());
412 CKHR(pRuntimeInfo->GetInterface(CLSID_CorRuntimeHost, IID_ICorRuntimeHost, (LPVOID*)&m_pHost));
414 CKHR(m_pHost->GetDefaultDomain(&pAppDomainPunk));
416 CKHR(pAppDomainPunk->QueryInterface(__uuidof(_AppDomain), (void**) &pDefaultDomain));
417 CK_ALLOC(pDefaultDomain);
419 EventWriteWpfHostUm_StartingCLREnd();
422 // Load and initialize the application.
424 Assert(m_pOleDoc->m_pAppBrowserHostServices == NULL);
425 //Load the app object thru interop
426 CKHR(pDefaultDomain->CreateInstance(bstrAssembly, bstrType, &pObjHandle));
427 pObjHandle->Unwrap(&v);
429 CKHR(v.pdispVal->QueryInterface(__uuidof(IBrowserHostServices),(void**) &(m_pOleDoc->m_pAppBrowserHostServices)));
431 if (m_pOleDoc->m_pAppBrowserHostServices == NULL)
433 CKHR(E_FAIL);
435 CKHR(m_pOleDoc->InitApplicationServer());
437 // Now we can do additional http web requests through ByteRangeDownloader (managed code)
438 if (m_pOleDoc->m_pByteWrapper)
440 CKHR(v.pdispVal->QueryInterface(__uuidof(IByteRangeDownloaderService),(void**) &(m_pOleDoc->m_pByteRangeDownloaderService)));
442 if (m_pOleDoc->m_pByteRangeDownloaderService == NULL)
444 CKHR(E_FAIL);
452 Cleanup:
453 HANDLE_ACTIVATION_FAULT(hr);
455 if (pDefaultDomain)
457 pAppDomainPunk->Release();
460 if (pAppDomainPunk)
462 pAppDomainPunk->Release();
465 if (pCLRDomainManager)
467 pCLRDomainManager->Release();
470 if (pCLRControl)
472 pCLRControl->Release();
475 if (pHost)
477 pHost->Release();
480 if (pRuntimeInfo)
482 pRuntimeInfo->Release();
485 if (pMetaHostPolicy)
487 pMetaHostPolicy->Release();
490 if (bstrAssembly != NULL)
492 SysFreeString(bstrAssembly);
495 if (bstrType != NULL)
497 SysFreeString(bstrType);